//
//  TableViewController.m
//  asdf
//
//  Created by 尤振宇 on 16/1/8.
//  Copyright © 2016年 尤振宇. All rights reserved.
//

#import "FatherTableViewController.h"

@interface FatherTableViewController ()
@property(nonatomic,strong)UIView *notDataView;
@property(nonatomic,assign)BOOL m_haveNotDataView;
@end

@implementation FatherTableViewController

-(instancetype)init
{
    if (self=[super initWithStyle:(UITableViewStyleGrouped)]) {

    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.tableView.backgroundColor = kF2F2F2;
    self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    self.tableView.bounces=NO;
    self.tableView.tableFooterView=[UIView new];
    self.tableView.cellLayoutMarginsFollowReadableWidth = NO;
  
    
}

-(void)configNotDataView:(UIImage *)image text:(NSString *)text
{
    if (!self.haveNotDataView) {
        UITapGestureRecognizer *zer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(refreshData)];
        [self.notDataView addGestureRecognizer:zer];
        
        UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
        imageView.userInteractionEnabled = YES;
        
        [self.tableView addSubview:self.notDataView];
        [self.notDataView addSubview:imageView];
        
        [self.notDataView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.equalTo(self.tableView);
            make.center.equalTo(self.tableView);
        }];
        
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(imageView.image.size);
            make.centerX.equalTo(self.notDataView);
            make.centerY.equalTo(self.notDataView).with.offset(-70);
        }];
        
        if (text) {
            UILabel *label = [[UILabel alloc]init];
            label.text = text;
            label.textColor = k666666;
            
            [self.notDataView addSubview:label];
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(self.tableView);
                make.top.equalTo(imageView.mas_bottom).with.offset(20);
            }];
        }
        
    }
    self.m_haveNotDataView = YES;
}

-(void)showOrHideDataView
{
    if (self.haveNotDataView) {
        if (self.dataArray.count == 0) {
            self.notDataView.hidden = NO;
        }else{
            self.notDataView.hidden = YES;
        }
    }
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (self.isViewWillAppearUpdata) {
        [self loadData];
        self.isViewWillAppearUpdata = NO;
    }
}

-(void)addMj_header
{
    [self.tableView setMj_header:[MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshData)]];
}

-(void)addMj_footer
{
    [self.tableView setMj_footer:[MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)]];
}

-(void)refreshData
{
    self.listApi.page = 1;
    [self loadData];
}

-(void)loadMore
{
    self.listApi.page ++ ;
    [self loadData];
}

-(void)loadData
{
    NSLog(@"子类重写");
}

-(void)endRefreshing
{
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if ([viewController isKindOfClass:[FatherTableViewController class]]) {
        FatherTableViewController *vc = (FatherTableViewController *)viewController;
        vc.updataTableViewController = self;
    }
    if ([viewController isKindOfClass:[FatherViewController class]]) {
        FatherViewController *vc = (FatherViewController *)viewController;
        vc.updataTableViewController = self;
    }
    [self.navigationController pushViewController:viewController animated:animated];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [UIView new];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}

-(NSMutableArray *)dataArray
{
    if (_dataArray==nil) {
        _dataArray=[NSMutableArray array];
    }
    return _dataArray;
}

-(BOOL)haveNotDataView
{
    return self.m_haveNotDataView;
}

-(void)dealloc
{
    NSLog(@"%@ 释放了!!!",[self class]);
}

-(UIView *)notDataView
{
    if (!_notDataView) {
        _notDataView = [UIView new];
        _notDataView.userInteractionEnabled = YES;
        _notDataView.backgroundColor = [UIColor whiteColor];
    }
    return _notDataView;
}


@end
